package com.UnionPayCenter.wechat;

import com.alibaba.fastjson.JSONObject;

import java.util.Date;

public class WeChatSystemContext {
	private JSONObject json = new JSONObject();

	static class WeChatSystemContextHolder {
		static WeChatSystemContext instance = new WeChatSystemContext();
	}

	public static WeChatSystemContext getInstance() {
		return WeChatSystemContextHolder.instance;
	}

	//是否过期
	public boolean isExpired(String appid) {
		JSONObject item = json.getJSONObject(appid);
		if(item != null) {
			Long createTime = item.getLong("createTime");
			long time = new Date().getTime();
			//如果当前记录时间为0
			if (createTime <= 0) {
				return true;
			}
			//判断记录时间是否超过7200s
			if (createTime / 1000 + 7200 < time / 1000) {
				return true;
			}
			return false;
		}else{
			return true;
		}
	}

	//记录接口访问凭证
	public void saveLocalAccessonToke(String appid, String accessToken) {
		JSONObject item = new JSONObject();
		item.put("accessToken", accessToken);
		item.put("createTime", new Date().getTime());
		json.put(appid, item);
	}

	public String getAccessToken(String appid) {
		JSONObject item = json.getJSONObject(appid);
		return item.getString("accessToken");
	}
}
